package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.TimeListDao;
import person.aoao.library.dao.TypeseatListDao;
import person.aoao.library.entity.TypeseatList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.TypeseatListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TypeseatList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-13 18:34:57
 */
@Service("typeseatListService")
public class TypeseatListServiceImpl implements TypeseatListService {
    @Resource
    private TypeseatListDao typeseatListDao;
    @Resource
    private TimeListDao timeListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param typeid 主键
     * @return 实例对象
     */
    @Override
    public TypeseatList queryById(Integer typeid) {
        return this.typeseatListDao.queryById(typeid);
    }

    /**
     * 分页查询
     *
     * @param typeseatList 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<TypeseatList> queryByPage(TypeseatList typeseatList, PageRequest pageRequest) {
        long total = this.typeseatListDao.count(typeseatList);
        return new PageImpl<>(this.typeseatListDao.queryAllByLimit(typeseatList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param typeseatList 实例对象
     * @return 实例对象
     */
    @Override
    public TypeseatList insert(TypeseatList typeseatList) {
        this.typeseatListDao.insert(typeseatList);
        return typeseatList;
    }

    /**
     * 修改数据
     *
     * @param typeseatList 实例对象
     * @return 实例对象
     */
    @Override
    public TypeseatList update(TypeseatList typeseatList) {
        this.typeseatListDao.update(typeseatList);
        return this.queryById(typeseatList.getTypeid());
    }

    /**
     * 通过主键删除数据
     *
     * @param typeid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer typeid) {
        return this.typeseatListDao.deleteById(typeid) > 0;
    }

    @Override
    public ResponseData getSeatTypeList(Integer num, Integer page) {
        ListData data=new ListData();
        List<TypeseatList> list=this.typeseatListDao.getSeatTypeList(num,(page-1)*num);
        long count=this.typeseatListDao.getCount();
        for(TypeseatList item:list){
            item.setTimeList(this.timeListDao.queryById(item.getTimeid()));
        }
        data.setCount(count);
        data.setData(list);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }
}
